import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regular_PatternMatcher {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("User\\d");
        Matcher matcher = pattern.matcher("User13UseraaaUser345");

        /* matches()：根据正则判断字符串是否完全匹配 */
        System.out.println(matcher.matches());//false

        /* find()：根据正则判断是否存在匹配的子字符串，第一次find且存在第一个匹配的子字符串则返回true，第二次find且存在第二个匹配的子字符串则返回true */
        /* group()：根据正则返回匹配子字符串，第一次group返回第一个匹配的子字符串，第二次group返回第二个匹配的子字符串 */
        System.out.println(matcher.find());//true
        System.out.println(matcher.group());//User1
        System.out.println(matcher.find());//true
        System.out.println(matcher.group());//User3
        System.out.println(matcher.find());//false
        //System.out.println(matcher.group());//Exception in thread "main" java.lang.IllegalStateException: No match found

        Pattern pattern1 = Pattern.compile("User\\d");
        Matcher matcher1 = pattern.matcher("User13UseraaaUser345");
        while (matcher1.find()) {
            System.out.println(matcher1.group());//["User1", "User3"]
        }
    }
}
